<?php

require_once(APPPATH.'controllers/crons/process_cron_info.php');

class Process_thumb_info extends Process_cron_info{

	function Process_thumb_info(){
		parent::Process_cron_info();
	}

	function index(){
		$this->load->model('m_channel_thumb');
		$this->run("SUITE_TICKET_LIVE_THUMB",array("live_thumb"));
	}

	function process_response($response){
		try{
			$live_thumb_info = $response["resultset"];
			$thumb = new Thumb();
			echo "\tthumbs: ". count($live_thumb_info)."\n";
			foreach($live_thumb_info as $row){
				if($row['action']!='del'){
					$thumb->channel_suite_id= $row['signal_id'];
					$thumb->date            = date_create_from_format('Y-m-d H:i:s',$row['date']);

					$dst_img = imagecreatetruecolor(80, 60);

					$content = $row['thumb_content'];
					$src_img = imagecreatefromstring(base64_decode($content));
					$width = imagesx($src_img);
					$height = imagesy($src_img);
					if(imagecopyresized($dst_img, $src_img, 0, 0, 0, 0, 80, 60, $width, $height)){
						if(empty ($dst_img)){
							echo 'El thumb está vacío...';
							continue;
						}else{
							ob_start();
							imagepng($dst_img);
							$image = ob_get_contents();
							ob_end_clean();
							$thumb->content = base64_encode($image);
						}
					}else{
						echo "No se pudo procesar el thumb del canal $thumb->channel_suite_id \n";
						continue;
					}
					$this->m_channel_thumb->replaceOldestThumb($thumb);
				}
			}

		}catch(Exception $ex){
			throw $ex;
		}
	}

}
?>
